<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Lib\Response;
use App\Http\Model\Hint;
use App\Http\Services\UserServices;
use App\Http\Services\MallServices;
use App\Http\Services\RoomServicesInit;
use App\Http\Services\RoomMsgServices;

class ApiMallGoodsController extends BaseController
{
    protected $userService;
    protected $mallService;
    protected $services_room_user;
    protected $services_room_msg;

    public function __construct(){
        $this->userService = new UserServices();
        $this->mallService = new MallServices();
        $this->services_room_user = RoomServicesInit::createRoomUser();
        $this->services_room_msg = new RoomMsgServices();

    }

    /**
     * 拥有的有效商品列表
     */
    public function user_goods_list(Request $request)
    {
        $loginUserInfo = $this->getLoginUserInfo();
        if(empty($loginUserInfo['user_id'])) {
            Response::returnResult(Hint::LOGIN_OUT);
        }

        $page = (int) $request->input('page_index', 0);
        $page_size = (int) $request->input('page_size', 10);
        $goods_type = (int) $request->input('goods_type');
        $goods_status = (string) $request->input('goods_status', 'valid');//expire无效 valid有效 all全
        if (empty($goods_type)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $list = $this->mallService->getUserGoodsList($loginUserInfo['user_id'], $goods_type, $page, $page_size, $goods_status);
        Response::returnResult(Hint::SUCCESS, $list);
    }

    /**
     * @desc DIY装扮
     * @param int $goods_id
     * */
    public function diy_attire(Request $request)
    {
        $loginUserInfo = $this->getLoginUserInfo();
        if(empty($loginUserInfo['user_id'])) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $userId = $loginUserInfo['user_id'];

        $id = (int) $request->input('id', 0);
        $upgrade_level = (int) $request->input('upgrade_level', 0);
        if(empty($id)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //装扮
        $goods_type = null;
        $ret = $this->mallService->setGoodsAttireService($userId, $id, $goods_type, $upgrade_level);
        if($ret != Hint::SUCCESS ){
            Response::returnResult($ret);
        }

        $room = $this->services_room_user->get_cur_room($userId);
        $room_id = empty($room) ? 0 : $room['room_id'];
        if (!empty($room_id) && $goods_type == 1){
            $this->services_room_msg->broadcast_update_coronet($room_id, $userId);
        }

        Response::returnResult(Hint::SUCCESS);
    }

    /**
     * 取消装扮
     */
    public function diy_cancel(Request $request)
    {
        $loginUserInfo = $this->getLoginUserInfo();
        if(empty($loginUserInfo['user_id'])) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $userId = $loginUserInfo['user_id'];

        $type = (int) $request->input('type', 0);
        if(empty($type)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        //取消装扮装扮
        $ret = $this->mallService->cancelGoodsAttireService($type, $userId);

        $room = $this->services_room_user->get_cur_room($userId);
        $room_id = empty($room) ? 0 : $room['room_id'];
        if (!empty($room_id) && $type == 1){
            $this->services_room_msg->broadcast_cancel_coronet($room_id, $userId);
        }

        Response::returnResult(Hint::SUCCESS);
    }

    /**
     * @desc 用户当前使用的装扮
     * */
    public function get_user_attire()
    {
        $loginUserInfo = $this->getLoginUserInfo();
        if(empty($loginUserInfo['user_id'])) {
            Response::returnResult(Hint::LOGIN_OUT);
        }

        $ret = $this->mallService->getUsedAttireService($loginUserInfo['user_id']);
        Response::returnResult(Hint::SUCCESS, $ret);
    }


}
